package com.github.shiqiyue.app.config.security.sapi;


import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.result.ResultCode;
import com.github.shiqiyue.app.common.result.ResultMsg;
import com.github.shiqiyue.app.common.utils.ResponseUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/***
 * 后台用户访问拒绝处理
 * @author wenyao.wu
 */
public class SapiAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        // 没权限，访问拒绝处理
        ResponseUtils.populateResponse(
                ResponseEntity.ok(Result.newInstance(ResultMsg.CAN_NOT_ACCESS, ResultCode.PERMISSION_DENY)),
                httpServletResponse);
    }
}
